﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Scan
{
    public partial class ImageButton : Control
    {
        public ImageButton()
        {
        }

        Image backgroundImage;
        bool pressed = false;
        int imageIndex = 0;

        // Property for the background image to be drawn behind the button text.
        public Image BackgroundImage
        {
            get
            {
                return this.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        public int ImageIndex
        {
            get
            {
                return this.imageIndex;
            }
            set
            {
                this.imageIndex = value;
            }
        }


        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.pressed = true;
            this.Invalidate();
            base.OnMouseDown(e);
        }

        // When the mouse is released, reset the "pressed" flag 
        // and invalidate to redraw the button in the unpressed state.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.pressed = false;
            this.Invalidate();
            base.OnMouseUp(e);
        }

        // Override the OnPaint method to draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(this.Parent.BackColor), e.ClipRectangle);

            if (this.backgroundImage != null)
            {
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);

                Rectangle rect = this.ClientRectangle;
                if (this.pressed)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(153, 217, 234)), rect);
                    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0, 128, 255)), rect);

                    rect.Offset(1, 1);
                }

                e.Graphics.DrawImage(this.backgroundImage, rect, imageIndex * this.backgroundImage.Height, 0, this.backgroundImage.Height, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);

            }

            base.OnPaint(e);
        }
    }
}
